package com.chiancloud.shield.common.params;

import com.chiancloud.shield.core.exception.BadCommandArgumentsException;
import com.chiancloud.shield.core.exception.ErrorStrings;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.*;


public class ArgOps {

  private static final Logger
    log = LoggerFactory.getLogger(ArgOps.class);

  public static List<Object> triple(String msg, int min, int max) {
    List<Object> l = new ArrayList<>(3);
    l.add(msg);
    l.add(min);
    l.add(max);
    return l;
  }

  public static List<Object> tuple(String msg, int min) {
    return triple(msg, min, min);
  }

  public static void applyFileSystemURL(URI filesystemURL, Configuration conf) {
    if (filesystemURL != null) {
      FileSystem.setDefaultUri(conf, filesystemURL);
    }
  }

  public static void splitPairs(Collection<String> pairs,
                                Map<String, String> dest) {
    for (String prop : pairs) {
      String[] keyval = prop.split("=", 2);
      if (keyval.length == 2) {
        dest.put(keyval[0], keyval[1]);
      }
    }
  }


  public static void applyDefinitions(Map<String, String> definitionMap,
                                      Configuration conf) {
    for (Map.Entry<String, String> entry : definitionMap.entrySet()) {
      String key = entry.getKey();
      String val = entry.getValue();
      log.debug("configuration[{}]=\"{}\"", key, val);
      conf.set(key, val, "command line");
    }
  }


  public static Map<String, String> convertTupleListToMap(String description,
                                                          List<String> list) throws
          BadCommandArgumentsException {
    Map<String, String> results = new HashMap<>();
    if (list != null && !list.isEmpty()) {
      int size = list.size();
      if (size % 2 != 0) {
        throw new BadCommandArgumentsException(
          ErrorStrings.ERROR_PARSE_FAILURE + description);
      }
      for (int count = 0; count < size; count += 2) {
        String key = list.get(count);
        String val = list.get(count + 1);
        if (results.get(key) != null) {
          throw new BadCommandArgumentsException(
            ErrorStrings.ERROR_DUPLICATE_ENTRY + description
            + ": " + key);
        }
        results.put(key, val);
      }
    }
    return results;
  }

  public static Map<String, Map<String, String>> convertTripleListToMaps(String description,
                                                                         List<String> list) throws
                                                                                            BadCommandArgumentsException {
    Map<String, Map<String, String>> results =
      new HashMap<>();
    if (list != null && !list.isEmpty()) {
      int size = list.size();
      if (size % 3 != 0) {
        //wrong number of elements, not permitted
        throw new BadCommandArgumentsException(
          ErrorStrings.ERROR_PARSE_FAILURE + description);
      }
      for (int count = 0; count < size; count += 3) {
        String role = list.get(count);
        String key = list.get(count + 1);
        String val = list.get(count + 2);
        Map<String, String> roleMap = results.get(role);
        if (roleMap == null) {
          //demand create new role map
          roleMap = new HashMap<>();
          results.put(role, roleMap);
        }
        if (roleMap.get(key) != null) {
          throw new BadCommandArgumentsException(
            ErrorStrings.ERROR_DUPLICATE_ENTRY + description
            + ": for key " + key + " under " + role);
        }
        roleMap.put(key, val);
      }
    }
    return results;
  }
}
